package com.dzsw.netty.websocket;

import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
import io.netty.handler.codec.http.websocketx.WebSocketVersion;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;

@Slf4j
public class WsClient {

    public static void main(String[] args) throws Exception{
        WsClient client = new WsClient();
        client.connect0();
    }

    public void connect() throws Exception {
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        try {
            URI uri = new URI("ws://192.168.1.192:8899/test");
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(eventLoopGroup)
                    .channel(NioSocketChannel.class).
                    handler(new WebSocketClientInitializer());
            Channel channel = bootstrap.connect(uri.getHost(), uri.getPort()).sync().channel();
            channel.closeFuture().sync();
        } catch (InterruptedException | URISyntaxException e) {
            log.error("socket连接异常:{}",e);
            e.printStackTrace();
        } finally {
            eventLoopGroup.shutdownGracefully();
        }
    }

    public void connect0() throws Exception {
        //netty基本操作，线程组
        EventLoopGroup group = new NioEventLoopGroup();
        //netty基本操作，启动类
        Bootstrap boot = new Bootstrap();
        boot.option(ChannelOption.SO_KEEPALIVE, true)
                .option(ChannelOption.TCP_NODELAY, true)
                .group(group)
                .channel(NioSocketChannel.class)
                .handler(new WebSocketClientInitializer());
        //websocke连接的地址，/hello是因为在服务端的websockethandler设置的
        URI websocketURI = new URI("ws://localhost:8899/test");
//        HttpHeaders httpHeaders = new DefaultHttpHeaders();
        //进行握手
//        WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(
//                websocketURI,
//                WebSocketVersion.V13,
//                (String) null,
//                true,
//                httpHeaders);
        boot.connect(websocketURI.getHost(), websocketURI.getPort()).sync();
//        //客户端与服务端连接的通道，final修饰表示只会有一个
//        final Channel channel = boot.connect(websocketURI.getHost(), websocketURI.getPort()).sync().channel();
//        WebSocketClientHandler handler = (WebSocketClientHandler) channel.pipeline().get("hookedHandler");
//        ChannelFuture f = handshaker.handshake(channel);
//        //阻塞等待是否握手成功
//        f.sync().addListener((ChannelFuture future)->{
//            System.out.println(future.isSuccess());
//        });
//        System.out.println("握手成功");
//        //给服务端发送的内容，如果客户端与服务端连接成功后，可以多次掉用这个方法发送消息
////        sengMessage(channel);
    }
}
